<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
  import { getPatientListAPI,delPatientAPI } from '@/apis/patient';
import { useUserConsultStore } from '@/stores/consult';
import type { PatientListData } from '@/types/patient';
import { onReady, onShow } from '@dcloudio/uni-app';
import {  ref } from 'vue'

  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
        color: '',
        fontSize: '',
      },
    },
  ])
  // 获取患者列表
  const patientList = ref<PatientListData[]>([])
  const  getPatientList = async ()=>{
    const res = await getPatientListAPI()
    // console.log(res);
    patientList.value = res.data
  }
  onShow(()=>{
    getPatientList()
  })
  // 删除患者
  const onSwipeActionClick = async (id: string, index: number) => {
  // 前端删除
  patientList.value.splice(index, 1)
  // 后端删除
  await delPatientAPI(id)
  // 提示
  uni.showToast({ title: '删除成功', icon: 'success' })
   // 如果删除的是当前选中的患者，需清空 store 中的 patientId
  if(store.patientId===id){
    store.patientId = ''
  }
}
// 获取页面参数
const props = defineProps<{
  type?: 'consult'
}>()
// console.log(props)
onReady(() => {
  // 动态设置标题写到 onReady 中兼容性更好
  uni.setNavigationBarTitle({ title: props.type === 'consult' ? '选择患者' : '患者列表' })
})
// 选择患者
const store = useUserConsultStore()
const handleSelectPatient = (id: string) => {
  // 如果不是选择患者，则不进行操作
  if (props.type !== 'consult') return
  // 保存患者 id 到 store
  store.patientId = id
  // console.log(store.patientId)
}
const goToPayment = () => {
    if (!store.patientId) return uni.showToast({ title: '请选择患者！', icon:'none' })
    uni.navigateTo({ url: '/subpkg_consult/pages/payment/payment' })
  }
</script>

<template>
  <scroll-view>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item 
        :right-options="swipeOptions"
        v-for="(item,index) in patientList" :key=item.id
        @click="onSwipeActionClick(item.id,index)"
        >
          <!-- 
            患者高亮的两种情况：
              选择患者：根据 patientId 高亮
              患者列表：根据 defaultFlag 高亮
          -->
          <view
            class="archive-card"
            @click="handleSelectPatient(item.id)"
            :class="{
              active:
                props.type === 'consult' ? item.id === store.patientId : item.defaultFlag === 1,
            }"
          >
            <view class="archive-info">
              <text class="name">{{item.name}}</text>
              <text class="id-card">
                <!-- 身份证号 替换部分敏感信息 -->
                {{item.idCard.replace(/^(.{6}).+(.{4})$/,'$1********$2')}}</text>
              <text v-if="item.defaultFlag===1" class="default">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{item.genderValue}}</text>
              <text class="age">{{item.age}}岁</text>
            </view>
            <navigator hover-class="none" class="edit-link" 
            :url="`/subpkg_archive/pages/form/form?id=${item.id}`">
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patientList.length<6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/pages/form/form">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
      <!-- 下一步按钮 -->
      <view class="next-step" v-if="props.type === 'consult'">
        <button 
        @click="goToPayment"
        :disabled="store.patientId === ''"
         class="uni-button">下一步</button>
      </view>
    </view>
  </scroll-view>
</template>

<style lang="scss">
  @import './index.scss';
</style>